package com.stylefeng.guns.modular.flagglory.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.persistence.model.Department;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.PoiUtil;
import com.stylefeng.guns.modular.flagglory.service.IDepartmentService;
import com.stylefeng.guns.modular.flagglory.warpper.InvitationCodeWarpper;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.common.persistence.model.InvitationCode;
import com.stylefeng.guns.modular.flagglory.service.IInvitationCodeService;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 邀请码控制器
 *
 * @author fengshuonan
 * @Date 2018-01-31 14:15:40
 */
@Controller
@RequestMapping("/invitationCode")
public class InvitationCodeController extends BaseController {

    private String PREFIX = "/flagGlory/invitationCode/";

    @Autowired
    private IInvitationCodeService invitationCodeService;
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 跳转到邀请码首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "invitationCode.html";
    }

    /**
     * 跳转到添加邀请码
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.GET)
    public String distribution() {
        return PREFIX + "distribution.html";
    }


    /**
     * 获取邀请码列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = invitationCodeService.selectInvitationByDept();
        return super.warpObject(new InvitationCodeWarpper(list));

    }

    /**
     * 新增邀请码
     */
    @RequestMapping(value = "/distribution", method = RequestMethod.POST)
    @ResponseBody
    public Object distribution(Integer deptid, Integer number) throws Exception {
        if (null == deptid) {
            throw new Exception("请选择部门！");
        }
        if (number == null) {
            throw new Exception("请输入正确的数量！");
        }
        Department department = departmentService.selectById(deptid);
        if (null == department) {
            throw new Exception("请选择正确的部门！");
        }
        if (department.getPid() == 0) {
            throw new Exception("请选择具体的子部门！");
        }
        invitationCodeService.distribution(deptid, number);
        return super.SUCCESS_TIP;
    }

  /*  */

    /**
     * 邀请码详情
     *//*
    @RequestMapping(value = "/detail/{deptId}")
    @ResponseBody
    public Object detail(@PathVariable("deptId") Integer deptId) {
        EntityWrapper<InvitationCode> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("deptid=" + deptId);
        return invitationCodeService.selectById(entityWrapper);
    }*/
    @RequestMapping(value = "/detail/{deptId}")
    public Object detail(@PathVariable("deptId") Integer deptId, Model model) {
        EntityWrapper<InvitationCode> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("deptid=" + deptId);
        List<InvitationCode> invitationCodes = invitationCodeService.selectList(entityWrapper);
        model.addAttribute("invitationCodes", invitationCodes);
        Department department = departmentService.selectById(deptId);
        model.addAttribute("department", department);
        return PREFIX + "detail.html";
    }

    @RequestMapping(value = "export")
    public void export(Integer deptId, HttpServletResponse response) {
        Department department = departmentService.selectById(deptId);
        EntityWrapper<InvitationCode> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("deptid=" + deptId);
        List<InvitationCode> invitationCodes = invitationCodeService.selectList(entityWrapper);
        PoiUtil.export(response, department.getFullname() + "部门邀请码", "邀请码", InvitationCode.class, invitationCodes);
    }
}
